package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.MarketAdminService;
import com.cskaoyan.market.service.impl.MarketAdminServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * ClassName: AdminSystemController
 * Package: com.cskaoyan.market.controller
 * Date: 2023/5/6 - 17:49
 * Description:
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    MarketAdminService marketAdminService ;

    @GetMapping("list")
    private Object list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userName = req.getParameter("username");
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)) {
            Object o = ResponseUtil.badArgument();
            return o;
        }
        if(StringUtils.isEmpty(sort)){
            sort="id";
        }
        if(StringUtils.isEmpty(order)){
            order="desc";
        }
        if (!("asc".equals(order) || "desc".equals(order))) {
            return ResponseUtil.badArgumentValue();
        }
        List<MarketAdmin> marketAdmins = marketAdminService.list(page, limit, sort, order,userName);
        if (marketAdmins.size() == 0) {
            Object o = ResponseUtil.badArgument();
            return o;
        }
        Object okList = ResponseUtil.okList(marketAdmins);
        return okList;
    }
    @PostMapping("update")
    private Object update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = getRequestBody(req);
        MarketAdmin marketAdmin = null;
        marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        if (StringUtils.isEmpty(marketAdmin.getUsername()) || StringUtils.isEmpty(marketAdmin.getPassword()) || marketAdmin.getId() == 0) {
            Object o = ResponseUtil.badArgument();
            return o;
        }
        MarketAdmin updataMarketAdmin = marketAdminService.update(marketAdmin);
        if (updataMarketAdmin == null) {
            Object o = ResponseUtil.updatedDataFailed();
            return o;
        }
        Object ok = ResponseUtil.ok(updataMarketAdmin);
        return ok;
    }
    @PostMapping("create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = getRequestBody(req);
        MarketAdmin marketAdmin = null;
        marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        if (StringUtils.isEmpty(marketAdmin.getUsername()) || StringUtils.isEmpty(marketAdmin.getPassword())) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        List<MarketAdmin> marketAdmins = marketAdminService.create(marketAdmin);
        if (marketAdmins == null) {
            Object o = ResponseUtil.badArgumentValue();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        Object ok = ResponseUtil.ok(marketAdmins);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private static String getRequestBody(HttpServletRequest req) throws IOException {
        ServletInputStream in = req.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = in.read(bytes)) != -1) {
            out.write(bytes, 0, length);
        }
        String requestBody = out.toString("utf-8");
        return requestBody;
    }
    @PostMapping("delete")
    private Object delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = getRequestBody(req);
        MarketAdmin marketAdmin = null;
        marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        int deleteIndex = marketAdminService.delete(marketAdmin);
        if (deleteIndex != 1) {
            Object o = ResponseUtil.badArgumentValue();
            return o;
        }
        Object o = ResponseUtil.ok(deleteIndex);
        return o;
    }
}
